package com.wyp.didishige.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.utils.DistanceUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.model.User;

import java.util.List;

/**
 * 地点检索
 * Created by horrywu on 15/10/13.
 */
public class PoiAdapter extends BaseAdapter {

    private List<PoiInfo> mList;
    private Context mContext;

    private User user;
    public PoiAdapter(Context context,List<PoiInfo> list,User user){
        this.mContext=context;
        this.mList=list;
        this.user =user;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setList(List<PoiInfo> list){
        mList=list;
        notifyDataSetChanged();
    }
    public void appendList(List<PoiInfo> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder=new ViewHolder();
            convertView  = LayoutInflater.from(mContext).inflate(R.layout.poi_item,null);
            holder.poiDistance = (TextView) convertView.findViewById(R.id.poiDistance);
            holder.poiName = (TextView) convertView.findViewById(R.id.poiName);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        PoiInfo info = mList.get(position);
        if (null !=info){
            holder.poiName.setText(info.name);
            holder.poiDistance.setText(evaluateDis(info.location));
        }
        return convertView;
    }

    private String evaluateDis(LatLng latLng){
        int meter = (int) DistanceUtil.getDistance(latLng, new LatLng(user.getLocation().getLatitude(), user.getLocation().getLongitude()));
        double distance = meter / 1000;
        if (meter<1000){
            return meter+"m";
        }else{
            return distance + "km";
        }
    }
    class ViewHolder{
        TextView poiName;
        TextView poiDistance;
    }

}
